package com.opensky.qatest.ui.page;

import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.LoadableComponent;

import com.opensky.qatest.ui.component.HomePageSignUpForm;
import com.opensky.qatest.util.common.LogicUtils;
import com.opensky.qatest.util.common.Properties;
import com.opensky.qatest.util.junit.Assert;
import com.opensky.qatest.util.selenium.WebDriverWrapper;

/*
 * OpenSky - "Home page" model
 *
 * @author: Aliakse_Khursevich (hursevich@gmail.com)
 * @version: 1.0
 * 
 */
public class HomePage extends LoadableComponent<HomePage> implements IPage {

    // ************************ Locators ************************

    private static final String HOME_PAGE_URL = Properties.get("env.root.url");

    private static final String LOGIN_LINK = "//div[@class='login-box']//a[@href='/login']";

    private static final String LOGO = "//a[@class='logo']/img[@src='img/opensky-logo-blue.png']";

    private static final String COPYRIGHT = "//div[@class='copyright']";

    private static final String HOWITWORKS_BTN = "//a[@class='button' and contains(., 'How it Works')]";

    private static final String BOTTOM_NAV = "//div[@class='boilerplate']/ul[@class='links']";

    private static final String GUARANTEE_LINK = "//span[@class='guarantee']/a[contains(., '90 Day Guarantee')]";

    private static final String HELP_LINK = "//footer//a[text()='help@opensky.com']";

    private static final String PHONE = "//footer//span[text()='1 (877) 734-6736']";

    // ************************ Complex page objects **************************

    private HomePageSignUpForm signUpForm;

    private final WebDriverWrapper driver;

    public HomePage(WebDriverWrapper driver) {
	this.driver = driver;
	signUpForm = new HomePageSignUpForm(driver);
    }

    // ************************ Actions **************************

    @Override
    protected void load() {
	driver.get(HOME_PAGE_URL);
    }

    public LoginPage clickLogInLink() {
	LoginPage loginPage = new LoginPage(driver);
	driver.waitAndClick("LOGIN_LINK", By.xpath(LOGIN_LINK));
	return loginPage.isPageLoaded() ? loginPage : null;
    }

    public HowItWorksPage clickHowItWorksBtn() {

	HowItWorksPage howItWorksPage = new HowItWorksPage(driver);
	driver.waitAndClick("HOWITWORKS_BTN", By.xpath(HOWITWORKS_BTN));
	return howItWorksPage.isPageLoaded() ? howItWorksPage : null;
    }

    // ************************ Validations **************************

    @Override
    protected void isLoaded() {
	Assert.assertUrlEqual(HOME_PAGE_URL, driver.getCurrentUrl());
    }

    @Override
    public boolean isPageLoaded() {
	return driver.waitAndVerifyURL(HOME_PAGE_URL);
    }

    public boolean isLogInLinkPresent() {
	return driver.waitAndVerifyElementPresent("LOGIN_LINK", By.xpath(LOGIN_LINK));
    }

    public boolean isLogoAndCopyrightsPresent() {

	return LogicUtils.isAllTrue(driver.waitAndVerifyElementPresent("LOGO", By.xpath(LOGO)),
		driver.waitAndVerifyElementPresent("COPYRIGHT", By.xpath(COPYRIGHT)));
    }

    public boolean isFooterElementsPresent() {

	return LogicUtils.isAllTrue(driver.waitAndVerifyElementPresent("HOWITWORKS_BTN", By.xpath(HOWITWORKS_BTN)),
		driver.waitAndVerifyElementPresent("BOTTOM_NAV", By.xpath(BOTTOM_NAV)),
		driver.waitAndVerifyElementPresent("GUARANTEE_LINK", By.xpath(GUARANTEE_LINK)),
		driver.waitAndVerifyElementPresent("HELP_LINK", By.xpath(HELP_LINK)),
		driver.waitAndVerifyElementPresent("PHONE", By.xpath(PHONE)));
    }

    // ************************ GET/SET methods **************************

    public HomePageSignUpForm getSignUpForm() {
	return signUpForm.isComponentLoaded() ? signUpForm : null;
    }

    @Override
    public String getName() {
	return "Home page";
    }
}
